/*
   Copyright 2009 Bloo. All Rights Reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.youtensils.www.client.data.youtube;

import java.util.ArrayList;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;

/**
 * A collection of YouTube GData movie search results.
 * @author Bobby Soares
 */
public class YouTubeSearchResults {
	protected int totalResults, totalPages, resultIndex, pageIndex, resultsPerPage;
	protected YouTubeMovie[] entries;
	/**
	 * The default constructor.
	 */
	public YouTubeSearchResults(){
		this.totalResults = 0;
		this.totalPages = 0;
		this.resultIndex = 0;
		this.pageIndex = 0;
		this.resultsPerPage = 0;
		this.entries = new YouTubeMovie[0];
	}
	/**
	 * Retrieves the total number of search result pages available.
	 * @return the number of search result pages
	 */
	public int getTotalPages() {
		return totalPages;
	}
	/**
	 * Retrieves the current start index for the search results.
	 * @return the current start index
	 */
	public int getResultIndex() {
		return resultIndex;
	}
	/**
	 * Retrieves the current page index for the search results.
	 * @return the current page index
	 */
	public int getPageIndex() {
		return pageIndex;
	}
	/**
	 * Retrieves the total number of search result entries available.
	 * @return the number of search result entries
	 */
	public int getTotalResults() {
		return totalResults;
	}
	/**
	 * Retrieves the number of search result entries per page.
	 * @return the number of entries per page
	 */
	public int getResultsPerPage() {
		return resultsPerPage;
	}
	/**
	 * Retrieves the search result entries.
	 * @return the search result entries
	 */
	public YouTubeMovie[] getEntries() {
		return entries;
	}
	/**
	 * Builds a YouTube search results object from a YouTube GData JSON feed.
	 * @param value the JSON object containing the YouTube search results
	 * @return the YouTube search results wrapped in a Java object
	 */
	public static YouTubeSearchResults fromFeed(JSONObject value){
		JSONObject feed = value.get("feed").isObject();
		if(feed == null) return new YouTubeSearchResults();
		JSONArray entries = feed.get("entry").isArray();
		if(entries == null) return new YouTubeSearchResults();
		ArrayList<YouTubeMovie> videos = new ArrayList<YouTubeMovie>();
		for(int i=0; i<entries.size(); i++){
			JSONObject entry = entries.get(i).isObject();
			if(entry == null) continue;
			videos.add(new YouTubeMovie(entry));
		}
		YouTubeSearchResults res = new YouTubeSearchResults();
		res.entries = videos.toArray(new YouTubeMovie[]{});
		res.totalResults = (int)YouTubeLoader.getNumericAttribute(feed, new String[]{ "openSearch$totalResults", "$t"});
		res.resultIndex = (int)YouTubeLoader.getNumericAttribute(feed, new String[]{ "openSearch$startIndex", "$t"});
		res.resultsPerPage = (int)YouTubeLoader.getNumericAttribute(feed, new String[]{ "openSearch$itemsPerPage", "$t"});
		res.pageIndex = res.resultsPerPage > 0 ? res.resultIndex/res.resultsPerPage : 0;
		res.totalPages = res.resultsPerPage > 0 ? res.totalResults/res.resultsPerPage : 0;
		return res;
	}
}
